package hig.no.group;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/*public class DBAdapter {

	private static final String DB_NAME = "GPSLocation";
	private static final String DB_TABLE = "GPSLocation";
	private static final int DB_VERSION = 1;
	
	private static final String DB_CREATE = 
		"CREATE TABLE GPSLocation (" +
		"_id integer PRIMARY KEY AUTOINCREMENT," +
		"Latitude," +
		"Longitude);"/* + "Street);"*/;	
	
	/*private static final String DB_UPGRADE = 
		"DROP TABLE IF EXISTS GPSLocation";
	public static final String COL_LATITUDE = "latitude";
	public static final String COL_LONGITUDE = "longitude";
	//public static final String COL_STREET = "street";
	public static final String COL_ID = "_id";
	
	private SQLiteDatabase mDB;
	private DBHelper mDBHelper;
	private Context mCtx;
	
	private static class DBHelper extends SQLiteOpenHelper{
		public DBHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE);			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(DB_UPGRADE);
			onCreate(db);
		}		
	}
	
	public DBAdapter(Context ctx)
	{
		this.mCtx = ctx;
	}
	
	//OPEN METHOD AND CLOSE METHOD
	public DBAdapter open()
	{
		mDBHelper = new DBHelper(mCtx);
		mDB = mDBHelper.getWritableDatabase(); //important
		return this;
	}
	
	public void close()
	{
		mDBHelper.close();
	}
	
	//INSERT AND DELETE
	public long createGPSlocation(String latitude, String longitude/*, String street*//*)
	{
		ContentValues v = new ContentValues();
		v.put(COL_LATITUDE, latitude);
		v.put(COL_LONGITUDE, longitude);
		//v.put(COL_STREET, street);
		return mDB.insert(DB_TABLE, null, v);
	}
	
	public boolean deleteGPSlocation(long id)
	{
		return mDB.delete(DB_TABLE, COL_ID + "="+ id, null)>0;
	}
	
	public boolean deleteAllGPSlocations()
	{
		return mDB.delete(DB_TABLE, null, null)>0;
	}
	
	//get all GPSlocations
	public Cursor GetAllGPSlocations()
	{
		return mDB.query(DB_TABLE, new String[] {COL_ID, COL_LATITUDE, COL_LONGITUDE/*, COL_STREET},
						null, null, null, null, null);
	}
	
	//get a specific GPSlocation by id
	public Cursor getGPSlocation(long id)
	{
		Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {COL_ID,  COL_LATITUDE, COL_LONGITUDE/*, COL_STREET},
				COL_ID + "=" + id,
				null, null, null, null, null);
	
		if (mCursor!=null)
			mCursor.moveToFirst();
		return mCursor;
	}
	
	//finally update the GPSlocation
	public boolean updateGPSlocation(long id, String latitude, String longitude/*, String street)
	{
		ContentValues v = new ContentValues();
		v.put(COL_LATITUDE, latitude);
		v.put(COL_LONGITUDE, longitude);
		//v.put(COL_STREET, street);
		
		return mDB.update(DB_TABLE, v, COL_ID + "=" + id, null) > 0;
	}	
}*/
		
		public class DBAdapter {

			private static final String DB_NAME = null;
			private static final String DB_TABLE = "Bookmark";
			private static final int DB_VERSION = 1;
			
			private static final String DB_CREATE = 
				"CREATE TABLE Bookmark (" +
				"_id integer PRIMARY KEY AUTOINCREMENT," +
				"title text," +
				"url text);";	
			
			private static final String DB_UPGRADE = 
				"DROP TABLE IF EXISTS Bookmark";
			public static final String COL_TITLE = "title";
			public static final String COL_URL = "url";
			public static final String COL_ID = "_id";
			
			private SQLiteDatabase mDB;
			private DBHelper mDBHelper;
			private Context mCtx;
			
			private static class DBHelper extends SQLiteOpenHelper{
				public DBHelper(Context context) {
					super(context, DB_NAME, null, DB_VERSION);
				}

				@Override
				public void onCreate(SQLiteDatabase db) {
					db.execSQL(DB_CREATE);			
				}

				@Override
				public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
					db.execSQL(DB_UPGRADE);
					onCreate(db);
				}		
			}
			
			public DBAdapter(Context ctx)
			{
				this.mCtx = ctx;
			}
			
			//OPEN METHOD AND CLOSE METHOD
			public DBAdapter open()
			{
				mDBHelper = new DBHelper(mCtx);
				mDB = mDBHelper.getWritableDatabase(); //important
				return this;
			}
			
			public void close()
			{
				mDBHelper.close();
			}
			
			//INSERT AND DELETE
			public long createBookmark(String title, String url)
			{
				ContentValues v = new ContentValues();
				v.put(COL_TITLE, title);
				v.put(COL_URL, url);
				return mDB.insert(DB_TABLE, null, v);
			}
			
			public boolean deleteBookmark(long id)
			{
				return mDB.delete(DB_TABLE, COL_ID + "="+ id, null)>0;
			}
			
			public boolean deleteAllBookmark()
			{
				return mDB.delete(DB_TABLE, null, null)>0;
			}
			
			//get all bookmark
			public Cursor GetAllBookmarks()
			{
				return mDB.query(DB_TABLE, new String[] {COL_ID, COL_TITLE, COL_URL},
								null, null, null, null, null);
			}
			
			//get a specific bookmark by id
			public Cursor getBookmark(long id)
			{
				Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {COL_ID, COL_TITLE, COL_URL},
						COL_ID + "=" + id,
						null, null, null, null, null);
			
				if (mCursor!=null)
					mCursor.moveToFirst();
				return mCursor;
			}
			
			//finally update the bookmark
			public boolean updateBookmark(long id, String title, String url)
			{
				ContentValues v = new ContentValues();
				v.put(COL_TITLE, title);
				v.put(COL_URL, url);
				
				return mDB.update(DB_TABLE, v, COL_ID + "=" + id, null) > 0;
			}	
		}